{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NOTE:  DO NOT RUN THIS NOTEBOOK UNTIL THE PREVIOUS NOTEBOOK IS COMPLETE (~30 MINUTES)\n",
    "\n",
    "# PLEASE BE PATIENT."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Predict Customer Reviews with the Autopilot Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "import sagemaker\n",
    "import pandas as pd\n",
    "import json\n",
    "\n",
    "sess = sagemaker.Session()\n",
    "bucket = sess.default_bucket()\n",
    "role = sagemaker.get_execution_role()\n",
    "region = boto3.Session().region_name\n",
    "\n",
    "sm = boto3.Session().client(service_name=\"sagemaker\", region_name=region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store -r autopilot_endpoint_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    autopilot_endpoint_name\n",
    "    print(\"[OK]\")\n",
    "except NameError:\n",
    "    print(\"***************************************************************************\")\n",
    "    print(\"[ERROR] PLEASE WAIT FOR THE PREVIOUS NOTEBOOK TO FINISH *******************\")\n",
    "    print(\"[ERROR] OR THIS NOTEBOOK WILL NOT RUN PROPERLY ****************************\")\n",
    "    print(\"***************************************************************************\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(autopilot_endpoint_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wait for the Model to Deploy\n",
    "This may take 5-10 mins.  Please be patient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sm.get_waiter(\"endpoint_in_service\").wait(EndpointName=autopilot_endpoint_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = sm.describe_endpoint(EndpointName=autopilot_endpoint_name)\n",
    "status = resp[\"EndpointStatus\"]\n",
    "\n",
    "print(\"Arn: \" + resp[\"EndpointArn\"])\n",
    "print(\"Status: \" + status)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Our Model with Some Example Reviews\n",
    "Let's do some ad-hoc predictions on our model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sm_runtime = boto3.client(\"sagemaker-runtime\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_line_predict_positive = \"\"\"I loved it!\"\"\"\n",
    "\n",
    "response = sm_runtime.invoke_endpoint(\n",
    "    EndpointName=autopilot_endpoint_name, ContentType=\"text/csv\", Accept=\"text/csv\", Body=csv_line_predict_positive\n",
    ")\n",
    "\n",
    "response_body = response[\"Body\"].read().decode(\"utf-8\").strip()\n",
    "\n",
    "r = response_body.split(\",\")\n",
    "print(\"Predicated Star Rating Class: {} \\nProbability: {} \".format(r[0], r[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_line_predict_meh = \"\"\"It's OK.\"\"\"\n",
    "\n",
    "response = sm_runtime.invoke_endpoint(\n",
    "    EndpointName=autopilot_endpoint_name, ContentType=\"text/csv\", Accept=\"text/csv\", Body=csv_line_predict_meh\n",
    ")\n",
    "\n",
    "response_body = response[\"Body\"].read().decode(\"utf-8\").strip()\n",
    "\n",
    "r = response_body.split(\",\")\n",
    "print(\"Predicated Star Rating Class: {} \\nProbability: {} \".format(r[0], r[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "csv_line_predict_negative = \"\"\"It's pretty good.\"\"\"\n",
    "\n",
    "response = sm_runtime.invoke_endpoint(\n",
    "    EndpointName=autopilot_endpoint_name, ContentType=\"text/csv\", Accept=\"text/csv\", Body=csv_line_predict_negative\n",
    ")\n",
    "\n",
    "response_body = response[\"Body\"].read().decode(\"utf-8\").strip()\n",
    "\n",
    "r = response_body.split(\",\")\n",
    "print(\"Predicated Star Rating Class: {} \\nProbability: {} \".format(r[0], r[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary\n",
    "We used Autopilot to automatically find the best model, hyper-parameters, and feature-engineering scripts for our dataset.  \n",
    "\n",
    "Autopilot uses a transparent approach to generate re-usable exploration Jupyter Notebooks and transformation Python scripts to continue to train and deploy our model on new data - well after this initial interaction with the Autopilot service."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Release Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
